package com.wannengbang.storemobile.login;


import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.sdk.android.push.CloudPushService;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.google.gson.Gson;
import com.gyf.immersionbar.ImmersionBar;
import com.wannengbang.storemobile.R;
import com.wannengbang.storemobile.a_main.MainActivity;
import com.wannengbang.storemobile.base.BaseActivity;
import com.wannengbang.storemobile.base.DataCallBack;
import com.wannengbang.storemobile.bean.LoginBean;
import com.wannengbang.storemobile.bean.UserInfoBean;
import com.wannengbang.storemobile.event.LoginRoleEvent;
import com.wannengbang.storemobile.homepage.IncomingVersionActivity;
import com.wannengbang.storemobile.homepage.StoreDataActivity;
import com.wannengbang.storemobile.login.model.ILoginModel;
import com.wannengbang.storemobile.login.model.LoginModelImpl;
import com.wannengbang.storemobile.merchant.MerchantLoginActivity;
import com.wannengbang.storemobile.mine.UserAgreementActivity;
import com.wannengbang.storemobile.mine.model.MineModelImpl;
import com.wannengbang.storemobile.utils.SPManager;
import com.wannengbang.storemobile.utils.ToastUtil;
import com.wannengbang.storemobile.widget.ViewLoading;

import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.edit_mobile)
    EditText editMobile;
    @BindView(R.id.edit_pwd)
    EditText editPwd;
    @BindView(R.id.cb_remember)
    CheckBox cbRemember;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.tv_forget_pwd)
    TextView tvForgetPwd;
    @BindView(R.id.tv_register_account)
    TextView tvRegisterAccount;
    @BindView(R.id.tv_merchant_self_registration)
    TextView tv_merchant_self_registration;
    @BindView(R.id.cb_display)
    CheckBox cbDisplay;
    @BindView(R.id.tv_service_agreement)
    TextView tv_service_agreement;
    @BindView(R.id.tv_privacy_policy)
    TextView tv_privacy_policy;
    @BindView(R.id.cb_agreement)
    CheckBox cb_agreement;

    private ILoginModel loginModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        registerEventBus();
        ImmersionBar.with(this)
                .statusBarDarkFont(true)
                .navigationBarDarkIcon(true)
                .navigationBarColor(R.color.white)
                .statusBarDarkFont(true, 0.2f)
                .init();
        initView();
    }

    @Subscribe
    public void onLoginRoleEvent(LoginRoleEvent event) {
        finish();
    }

    public void initView() {
        loginModel = new LoginModelImpl();
        //点击眼隐藏或显示密码
        cbDisplay.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    //选择状态 显示明文--设置为可见的密码
                    editPwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    editPwd.setSelection(editPwd.getText().toString().length());
                } else {
                    //默认状态显示密码--设置文本 要一起写才能起作用 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD
                    editPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    editPwd.setSelection(editPwd.getText().toString().length());
                }
            }
        });
        String moblie = SPManager.getInstance().getUserMobile();
        String pwd = SPManager.getInstance().getUserPwd();
        if (!TextUtils.isEmpty(pwd)) {
            editMobile.setText(moblie);
            editMobile.setSelection(moblie.length());

            editPwd.setText(pwd);
            editPwd.setSelection(pwd.length());
            cbRemember.setChecked(true);
        }
    }

    public void requesLogin() {
        String mobile = editMobile.getText().toString();
        String password = editPwd.getText().toString();
        if (TextUtils.isEmpty(mobile)) {
            ToastUtil.showShort("请输入手机号");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            ToastUtil.showShort("请输入密码");
            return;
        }
        ViewLoading.showProgress(mActivity, "登录中......");
        CloudPushService pushService = PushServiceFactory.getCloudPushService();
        loginModel.requestLoginByPwd(mobile, password, pushService.getDeviceId(), new DataCallBack<LoginBean>() {
            @Override
            public void onSuccessful(LoginBean bean) {

                SPManager.getInstance().saveRoleType("2");
                SPManager.getInstance().saveToken(bean.getData().getAccess_token());
                ToastUtil.showShort(bean.getMsg());
                requestUserInfo();
                SPManager.getInstance().saveUserMobile(mobile);
                if (cbRemember.isChecked()) {
                    SPManager.getInstance().saveUserPwd(password);
                } else {
                    SPManager.getInstance().saveUserPwd("");
                }
                showActivity(MainActivity.class);
                finish();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    public void requestUserInfo() {
        new MineModelImpl().requestMine(new DataCallBack<UserInfoBean>() {
            @Override
            public void onSuccessful(UserInfoBean bean) {

                Log.e("登录返回数据=", new Gson().toJson(bean));
                SPManager.getInstance().saveUserInfoJson(new Gson().toJson(bean));
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @OnClick({R.id.tv_login, R.id.tv_forget_pwd, R.id.tv_register_account, R.id.tv_merchant_login, R.id.tv_merchant_self_registration, R.id.tv_service_agreement, R.id.tv_privacy_policy})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.tv_login:

                boolean checked = cb_agreement.isChecked();
                if (!checked){
                    Toast.makeText(this, "请勾选同意《服务协议》和《隐藏政策》", Toast.LENGTH_SHORT).show();
                    return;
                }

                requesLogin();
                break;
            case R.id.tv_forget_pwd:
                showActivity(ForgetPwdActivity.class);
                break;
            case R.id.tv_register_account:
                showActivity(RegisterAccountActivity.class);
                break;
            case R.id.tv_merchant_login:
                showActivity(MerchantLoginActivity.class);
                break;
            case R.id.tv_merchant_self_registration:
                intent = new Intent(LoginActivity.this, IncomingVersionActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_service_agreement:
                intent = new Intent(mActivity, UserAgreementActivity.class);
                intent.putExtra("type", 1);
                startActivity(intent);
                break;
            case R.id.tv_privacy_policy:
                intent = new Intent(mActivity, UserAgreementActivity.class);
                intent.putExtra("type", 2);
                startActivity(intent);
                break;
        }
    }
}
